package com.wdc.deliveryrobot.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.activity.BaseActivity;
import com.wdc.deliveryrobot.dialog.HintDialog;

import java.util.List;

public class MapAdapter extends RecyclerView.Adapter<MapAdapter.ViewHolder>{

    private List<String> mapList;

    private Context context;

    private String mapName;

    private boolean fistSelect = false;

    private OnItemClickListener listener;
    private int selectedPosition = -1;
    public interface OnItemClickListener {
        void onItemClick(String location);
    }
    public MapAdapter(List<String> mapList,Context context,String mapName, OnItemClickListener listener){
        this.mapList = mapList;
        this.context = context;
        this.mapName = mapName;
        this.listener = listener;
    }
    @NonNull
    @Override
    public MapAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_map, parent, false);
        return new MapAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MapAdapter.ViewHolder holder, int position) {
        String name = mapList.get(position);
        holder.textView.setText(name.replace(".yaml",""));
        if (mapName.equals(name)){
            holder.imageView.setImageResource(R.drawable.map_iconing);
            holder.textView.setTextColor(Color.BLUE);
            holder.button.setEnabled(false);
        }
        if (position == selectedPosition || (mapName.equals(name) && !fistSelect)) {
            holder.textView.setTextColor(Color.BLUE); // 选中按钮的背景
            fistSelect = true;
        } else {
            holder.textView.setTextColor(Color.BLACK);// 其他按钮恢复白色背景
        }


        //切换地图
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HintDialog hintDialog = new HintDialog(context);
                hintDialog.setMessage(context.getString(R.string.or_map,name));
                hintDialog.setCancelButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        hintDialog.dismiss();
                    }
                });
                hintDialog.setOkButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (listener != null)
                            listener.onItemClick(name);
                        hintDialog.dismiss();
                    }
                });
                hintDialog.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mapList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public Button button;
        public TextView textView;

        public ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.switch_map);
            textView = itemView.findViewById(R.id.map_name);
            imageView = itemView.findViewById(R.id.mapView);
        }
    }


}
